/**
  ************************************* Copyright ******************************
  * FileName   : debug.c
  * Version    : v1.0
  * Author     : yychao
  * Date       : 2023-03-03
  * Description:
  * Function List:
  	1. ....
  	   <version>:
  <modify staff>:
  		  <data>:
   <description>:
  	2. ...
  ******************************************************************************
 */
#include "cJSON.h"
#include "log.h"
#include "debug.h"
#include "usart.h"
#include "string.h"
#include "adc_app.h"
#include "ds18b20.h"
#include "dma.h"
#include "lfs_port.h"
#include "gpio.h"
#include "soft_timer.h"
#include "rtc.h"
#include "io_ctrl.h"
/**********************************************************/

struct table cjson_tab;

/**********************************************************/
void debug_deal(usart_cache* recv) //DataBuff* recv
{

    cJSON* json;
    cJSON* date;
    cJSON* time;
    cJSON* d_value;
    cJSON* read;
    cJSON* write;
    cJSON* value;
    uint16_t array_num;
    cjson_tab.num = 0;

    const char* ptr = NULL;
    ptr = (const char*)&recv->data[3];
    json = cJSON_Parse(ptr);
    if (json == NULL) {
        LOG_DEBUG("json fmt error: \r\n %s\r\n", cJSON_GetErrorPtr());
        cJSON_Delete(json);
        return;
    }
//    LOG_DEBUG("json fmt ok\r\n");


    date = cJSON_GetObjectItem(json, "DATE");
    if (date != NULL) {
        write = cJSON_GetObjectItem(date, "W");
        if (write != NULL) {
            array_num = cJSON_GetArraySize(write);
            for(int cnt = 0; cnt < array_num; cnt++) {
                value = cJSON_GetArrayItem(write, cnt);
                if(value == NULL) {
                    continue;
                }
                switch(cnt) {
                case 0:
                    RTC_DateStructure.WeekDay = value->valueint;
                    break;
                case 1:
                    RTC_DateStructure.Date = value->valueint;
                    break;
                case 2:
                    RTC_DateStructure.Month = value->valueint;
                    break;
                case 3:
                    RTC_DateStructure.Year = value->valueint;
                    break;
                }
            }
            /* Configure the RTC date register */
            if (RTC_SetDate(RTC_FORMAT_BIN, &RTC_DateStructure) == ERROR)
            {
                LOG_DEBUG("RTC Set Date failed...\r\n");
                return;
            }
        }
        read = cJSON_GetObjectItem(date, "R");
        if (read != NULL) {
            if (0  == memcmp((const char*) read->valuestring, "Y", strlen("Y"))) {

                cjson_tab.res_table[cjson_tab.num].level = 2;
                cjson_tab.res_table[cjson_tab.num].tab_array[0] = 1;
                cjson_tab.res_table[cjson_tab.num].tab_array[1] = 1;
                cjson_tab.res_table[cjson_tab.num].tab_array[2] = 0;
                cjson_tab.num++;
            }
        }
    }
    time = cJSON_GetObjectItem(json, "TIME");
    if (time != NULL) {
        write = cJSON_GetObjectItem(time, "W");
        if (write != NULL) {
            array_num = cJSON_GetArraySize(write);
            for(int cnt = 0; cnt < array_num; cnt++) {
                value = cJSON_GetArrayItem(write, cnt);
                if(value == NULL) {
                    continue;
                }
                switch(cnt) {
                case 0:
                    RTC_TimeStructure.Hours = value->valueint;
                    break;
                case 1:
                    RTC_TimeStructure.Minutes = value->valueint;
                    break;
                case 2:
                    RTC_TimeStructure.Seconds = value->valueint;
                    break;
                }
            }
            /* Configure the RTC time register */
            if (RTC_ConfigTime(RTC_FORMAT_BIN, &RTC_TimeStructure) == ERROR)
            {
                LOG_DEBUG("RTC Set Time failed...\r\n");
                return;
            }
        }
        read = cJSON_GetObjectItem(time, "R");
        if (read != NULL) {
            if (0  == memcmp((const char*) read->valuestring, "Y", strlen("Y"))) {

                cjson_tab.res_table[cjson_tab.num].level = 2;
                cjson_tab.res_table[cjson_tab.num].tab_array[0] = 2;
                cjson_tab.res_table[cjson_tab.num].tab_array[1] = 1;
                cjson_tab.res_table[cjson_tab.num].tab_array[2] = 0;
                cjson_tab.num++;
            }
        }
    }
    d_value = cJSON_GetObjectItem(json, "D_VALUE");
    if (d_value != NULL) {
        write = cJSON_GetObjectItem(d_value, "W");
        if (write != NULL) {
            array_num = cJSON_GetArraySize(write);
            for(int cnt = 0; cnt < array_num; cnt++) {
                value = cJSON_GetArrayItem(write, cnt);
                if(value == NULL) {
                    continue;
                }
                switch(cnt) {
                case 0:
                    ctrl_state_st.now_dry_value = value->valueint;
                    ctrl_state_st.now_dry_value /= 10;
                    LOG_DEBUG("D_VALUE: %d \r\n", value->valueint);
                    LOG_DEBUG("now_dry_value: %f \r\n", ctrl_state_st.now_dry_value);
                    break;
                }
            }
        }
    }
    cJSON_Delete(json);

    cJSON* rjson;
    cJSON* rdate;
    cJSON* rtime;
    cJSON* rres_date;
    cJSON* rres_time;
    char* str = NULL;
    rjson = cJSON_CreateObject();
    if (rjson == NULL) {
        LOG_DEBUG("cJSON_CreateObject failed\r\n");
        cJSON_Delete(rjson);
        return;
    }
    for(uint8_t i = 0; i < cjson_tab.num; i++) {
        switch(cjson_tab.res_table[i].level) {
        case 1:
            switch(cjson_tab.res_table[i].tab_array[0]) {
            case 1:
                break;
            case 2:
                break;
            }
            break;
        case 2:
            switch(cjson_tab.res_table[i].tab_array[0]) {
            case 1:
                switch(cjson_tab.res_table[i].tab_array[1]) {
                case 1:
                    RTC_GetDate(RTC_24HOUR_FORMAT, &RTC_DateStructure);
                    rdate = cJSON_CreateObject();
                    cJSON_AddItemToObject(rjson, "DATE", rdate);
                    rres_date = cJSON_CreateArray();
                    cJSON_AddItemToArray(rres_date, cJSON_CreateNumber(RTC_DateStructure.WeekDay));
                    cJSON_AddItemToArray(rres_date, cJSON_CreateNumber(RTC_DateStructure.Date));
                    cJSON_AddItemToArray(rres_date, cJSON_CreateNumber(RTC_DateStructure.Month));
                    cJSON_AddItemToArray(rres_date, cJSON_CreateNumber(RTC_DateStructure.Year));
                    cJSON_AddItemToObject(rdate, "RES", rres_date);
                    break;
                case 2:
                    break;
                }
                break;
            case 2:

                switch(cjson_tab.res_table[i].tab_array[1]) {
                case 1:
                    RTC_GetTime(RTC_FORMAT_BIN, &RTC_TimeStructure);
                    rtime = cJSON_CreateObject();
                    cJSON_AddItemToObject(rjson, "TIME", rtime);
                    rres_time = cJSON_CreateArray();
                    cJSON_AddItemToArray(rres_time, cJSON_CreateNumber(RTC_TimeStructure.Hours));
                    cJSON_AddItemToArray(rres_time, cJSON_CreateNumber(RTC_TimeStructure.Minutes));
                    cJSON_AddItemToArray(rres_time, cJSON_CreateNumber(RTC_TimeStructure.Seconds));
                    cJSON_AddItemToObject(rtime, "RES", rres_time);
                    break;
                case 2:
                    break;
                }
                break;
            }
            break;
        case 3:
            switch(cjson_tab.res_table[i].tab_array[0]) {
            case 1:
                switch(cjson_tab.res_table[i].tab_array[1]) {
                case 1:
                    switch(cjson_tab.res_table[i].tab_array[2]) {
                    case 1:
                        break;
                    case 2:
                        break;
                    }
                    break;
                case 2:
                    break;
                }
                break;
            case 2:
                switch(cjson_tab.res_table[i].tab_array[1]) {
                case 1:
                    switch(cjson_tab.res_table[i].tab_array[2]) {
                    case 1:
                        break;
                    case 2:
                        break;
                    }
                case 2:
                    break;
                }
                break;
            }
            break;
        }
    }
    str = cJSON_Print(rjson);
    if(str != NULL) {
//        LOG_DEBUG("%s \r\n", str);
    } else {
        LOG_DEBUG("cJSON_Print NULL\r\n", str);
    }
    cJSON_free(str);
    cJSON_Delete(rjson);

}

